//
//  YYaoViewController.m
//  egt
//
//  Created by 布路宽 on 14/12/8.
//  Copyright (c) 2014年 布路宽. All rights reserved.
//
#import "MobClick.h"
#import "YYaoViewController.h"
#import "UIPopoverListView.h"
#import "ZCPrizeViewController.h"
#define Screen_Width  [UIScreen mainScreen].bounds.size.width
#define Screen_Height  [UIScreen mainScreen].bounds.size.height
@interface YYaoViewController ()<UIPopoverListViewDelegate>
@property (nonatomic,strong) UIPopoverListView *alertView;
@property (nonatomic,strong) __block NSString  *countStr;
@end

@implementation YYaoViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestyaoyiyaoCount];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    [self.navigationController setNavigationBarHidden:YES];
    [MobClick beginLogPageView:@"摇一摇"];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"摇一摇"];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.MeButton.layer.cornerRadius = 35/2;
    [self.MeButton setTitleColor:[UIColor colorWithHexString:@"EB2E4A"] forState:UIControlStateNormal];
    
    if (IS_IPHONE_4_OR_LESS) {
        self.ImageViewheght.constant = 480.0;
        [_yaoImageView setImage:[UIImage imageNamed:@"yaoyiyao4s.png"]];
        self.titleWidth.constant = 170;
        self.titleHeight.constant = 90;
        self.CountCenterY.constant = 80;
        self.shouCenterY.constant = 70;
        self.shouCenterX.constant = 40;
        self.shouImageWidth.constant = 140;
        self.shouImageHeight.constant = 140;
    }else if (IS_IPHONE_5){
        self.ImageViewheght.constant = 568.0;
        [_yaoImageView setImage:[UIImage imageNamed:@"yaoyiyao5s.png"]];
        self.titleWidth.constant = 189;
        self.titleHeight.constant = 100;
        self.CountCenterY.constant = 100;
        self.shouCenterY.constant = 70;
        self.shouCenterX.constant = 40;
        self.shouImageWidth.constant = 140;
        self.shouImageHeight.constant = 140;
    }else if (IS_IPHONE_6){
        self.ImageViewheght.constant = 667.0;
    }else{
        self.ImageViewheght.constant = 736.0;
    }

    self.title = @"摇红包";
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.shouImageView.layer.anchorPoint = CGPointMake(1, 1);
    NSString *path = [[NSBundle mainBundle] pathForResource:@"5012" ofType:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
    // 设置允许摇一摇功能
    [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
    // 并让自己成为第一响应者
    [self becomeFirstResponder];
}

#pragma mark - 摇一摇相关方法

// 摇一摇开始摇动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    if (![_alertView isDescendantOfView:keywindow])
    {
        CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        //设置抖动幅度
        shake.fromValue = [NSNumber numberWithFloat:-0.15];
        shake.toValue = [NSNumber numberWithFloat:+0.15];
        shake.duration = 0.1;
        shake.autoreverses = YES; //是否重复
        shake.repeatCount = 6;
        
        [self.shouImageView.layer addAnimation:shake forKey:nil];
        yyImgView.alpha = 1.0;
        [self requestyaoyiyao]; //摇一摇请求
    
    }
}

// 摇一摇取消摇动
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    return;
}

//摇一摇请求
-(void)requestyaoyiyao{
    AudioServicesPlaySystemSound (soundID);
    [NetworkManager requestAcsuzhouGetPrizemid:UserModel.mid password:UserModel.password success:^(NSDictionary *dict) {
        
        if ([[dict objectForKey:@"content"] isKindOfClass:[NSDictionary class]]){
            NSDictionary *countDict = (NSDictionary *)[dict objectForKey:@"content"];
            NSString * isprize = [NSString stringWithFormat:@"%@",[countDict objectForKey:@"is_prize"]];
            NSString * prizeleftcount = [NSString stringWithFormat:@"%@",[countDict objectForKey:@"prize_left_count"]];
            
            NSLog(@"ssss=%@",prizeleftcount);
            
            if ([prizeleftcount isEqualToString:@"0"]) {
                [[[iToast makeText:@"您的抽奖机会已用完"]setGravity:iToastGravityCenter] show];
            }else{
                if ([isprize isEqualToString:@"0"]) {
                    //没中奖
                    [self ClikLable:NO];
                }else{
                    [self ClikLable:YES];
                    [_alertView setImageURL:countDict[@"prize_thumb"]];
                }
            }
            NSString *numstr = [NSString stringWithFormat:@"您还有%@次抽奖机会",prizeleftcount];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:numstr];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#FFE21F"] range:NSMakeRange(3,1)];
            self.countLable.attributedText = str;
        }
    } failure:^(NSError *error) {
        NSLog(@"请求失败了 = %@",error);
    }];
}

-(void)requestyaoyiyaoCount{
    //请求摇一摇剩余次数
    [NetworkManager requestAcsuzhouPrizeRecordmid:UserModel.mid password:UserModel.password success:^(NSDictionary *dict) {
        
        if ([[dict objectForKey:@"content"] isKindOfClass:[NSDictionary class]]){
            NSDictionary *countDict = (NSDictionary *)[dict objectForKey:@"content"];
            NSString *activity_err =[NSString stringWithFormat:@"%@",countDict[@"activity_err"]];
            if ([activity_err isEqualToString:@"0"]) {
                NSString *prize_left_count = [NSString stringWithFormat:@"%@",countDict[@"prize_left_count"]];
                
                if ([prize_left_count isEqualToString:@"0"]) {
                    [[[iToast makeText:@"您的抽奖机会已用完"]setGravity:iToastGravityCenter] show];
                }
                NSString *numstr = [NSString stringWithFormat:@"您还有%@次抽奖机会",prize_left_count];
                NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:numstr];
                [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#FFE21F"] range:NSMakeRange(3,1)];
                self.countLable.attributedText = str;
            }else if ([activity_err isEqualToString:@"1"]){
                //未报名
                [[[iToast makeText:@"未报名"]setGravity:iToastGravityCenter] show];
                
            }else{
                //没有参加考试
                [[[iToast makeText:@"没有参加考试"]setGravity:iToastGravityCenter] show];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

-(void)ClikLable:(BOOL)isprize{
    [NSThread sleepForTimeInterval:1.0];//设置启动页面时间
    CGFloat xWidth = 244;
    CGFloat yHeight = 270;
    CGFloat yOffset = (self.view.bounds.size.height - yHeight)/2.0f-25;
    
    _alertView = [[UIPopoverListView alloc] initWithFrame:CGRectMake(10, yOffset, xWidth, yHeight)];
    [_alertView setTitle:@"Share to" and:isprize];
    _alertView.delegate = self;
    [_alertView show];
}

-(void)ShakeagainClickButton{
    NSLog(@"点击了再摇一次");
}
-(void)ImmediateuseClickButton{
      NSLog(@"点击了立即使用");
    [self.navigationController popToRootViewControllerAnimated:NO];
}

#pragma mark 返回按钮
- (IBAction)ClickBackButton:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 我的奖品
- (IBAction)ClickMeButton:(UIButton *)sender {
    NSLog(@"我的奖品");

    ZCPrizeViewController *prizeVC = [[ZCPrizeViewController alloc]init];
    prizeVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:prizeVC animated:YES];
}

@end
